//
//  AppDelegate.m
//  彩虹儿歌
//
//  Created by lanou3g on 15/6/30.
//  Copyright (c) 2015年 HelloBaby. All rights reserved.
//

#import "AppDelegate.h"
//#import "MusicHomePageVC.h"
//#import "MovieShowViewController.h"
//#import "DownLoadVC.h"
//#import "UserVC.h"
//#import "PlayerView.h"
#import "MacroHeader.h"
#import "KKNavigationController.h"
#import "TabBarViewController.h"
@interface AppDelegate ()

@property (nonatomic, assign) BOOL isShowPlayerShowV;
@property (nonatomic, retain) UIView *backV;

@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [_playerV release];
    [_playShowV release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    TabBarViewController *tabBarVC= [[TabBarViewController alloc] init];
    tabBarVC.delegate = self;
    
    KKNavigationController *navigationVC = [[KKNavigationController alloc] initWithRootViewController:tabBarVC];
    navigationVC.navigationBar.translucent = NO;
    tabBarVC.navigationItem.title = @"听儿歌";
    
    self.window.rootViewController = navigationVC;
    
    
    
    CGFloat playerVF = SCREEN_WIDTH / 5;
    
    self.playerV = [[PlayerView alloc] initWithFrame:CGRectMake(playerVF * 2, SCREEN_HEIGHT - playerVF, playerVF, playerVF * 2)];
    [self.playerV.playerBtn setImage:[UIImage imageNamed:@"bofang04blue"] forState:UIControlStateNormal];
    [self.playerV.playerBtn addTarget:self action:@selector(showPlayView:) forControlEvents:UIControlEventTouchUpInside];

//    self.playerV.layer.shadowColor = [[UIColor blackColor] CGColor];
//    self.playerV.layer.shadowOffset = CGSizeMake(0, 0);
//    self.playerV.layer.shadowRadius = 20;
//    self.playerV.layer.shadowOpacity = 1.5;

    self.playShowV = [[PlayerShowView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT / 2 + 40)];
    self.playShowV.backgroundColor = [UIColor yellowColor];

//    self.playShowV.layer.shadowColor = [[UIColor blackColor] CGColor];
//    self.playShowV.layer.shadowOffset = CGSizeMake(0, 0);
//    self.playShowV.layer.shadowRadius = 20;
//    self.playShowV.layer.shadowOpacity = 1.5;
    
    _isShowPlayerShowV = NO;
    [self.window addSubview:self.playerV];
    [self.window addSubview:self.playShowV];


    self.backV = [[UIView alloc] initWithFrame:self.window.bounds];
    self.backV.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [self addTapGesture];
    
    [self.playerV release];
    [self.playShowV release];
    [self.backV release];
    
    return YES;
}

- (void)addTapGesture
{
    UITapGestureRecognizer *tapGesture = [[ UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPlayView:)];
    [self.backV addGestureRecognizer:tapGesture];
}

- (void)showPlayView:(UIButton *)button
{
    
    CGFloat playerVF = SCREEN_WIDTH / 5;
    if (_isShowPlayerShowV == NO) {
        
        self.playerV.layer.shadowColor = [[UIColor blackColor] CGColor];
        self.playerV.layer.shadowOffset = CGSizeMake(0, 0);
        self.playerV.layer.shadowRadius = 20;
        self.playerV.layer.shadowOpacity = 1.5;
        
        self.playShowV.layer.shadowColor = [[UIColor blackColor] CGColor];
        self.playShowV.layer.shadowOffset = CGSizeMake(0, 0);
        self.playShowV.layer.shadowRadius = 20;
        self.playShowV.layer.shadowOpacity = 1.5;
        
        [self.window addSubview:self.backV];
        [self.window bringSubviewToFront:self.playerV];
        [self.window bringSubviewToFront:self.playShowV];
        
        //回弹动画
        
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseOut animations:^{
            self.playerV.playerBtn.layer.transform = CATransform3DMakeRotation(M_PI / 2 * 1.5, 0, 0, 1);
            self.playerV.frame = CGRectMake(playerVF * 2, SCREEN_HEIGHT / 2 - 50 - playerVF, playerVF, playerVF);
            self.playShowV.frame = CGRectMake(0, SCREEN_HEIGHT / 2 - 50, SCREEN_WIDTH, SCREEN_HEIGHT / 2 + 50);
        } completion:nil];
        [UIView animateWithDuration:0.15 delay:0.2 options:UIViewAnimationOptionBeginFromCurrentState |  UIViewAnimationOptionCurveEaseOut   animations:^{
            self.playerV.playerBtn.layer.transform = CATransform3DMakeRotation(M_PI / 2, 0, 0, 1);
            self.playerV.frame = CGRectMake(playerVF * 2, SCREEN_HEIGHT / 2 + 20 - playerVF, playerVF, playerVF);
            self.playShowV.frame = CGRectMake(0, SCREEN_HEIGHT / 2 + 20,  SCREEN_WIDTH, SCREEN_HEIGHT / 2 + 50);
        } completion:nil];
        [UIView animateWithDuration:0.2 delay:0.35 options:UIViewAnimationOptionBeginFromCurrentState |  UIViewAnimationOptionCurveEaseOut   animations:^{
            self.playerV.frame = CGRectMake(playerVF * 2, SCREEN_HEIGHT / 2 - 10 - playerVF, playerVF, playerVF);
            self.playShowV.frame = CGRectMake(0, SCREEN_HEIGHT / 2 - 10,  SCREEN_WIDTH, SCREEN_HEIGHT / 2 + 50);
        } completion:nil];
        [UIView animateWithDuration:0.1 delay:0.55 options:UIViewAnimationOptionBeginFromCurrentState |  UIViewAnimationOptionCurveEaseOut   animations:^{
            self.playerV.frame = CGRectMake(playerVF * 2, SCREEN_HEIGHT / 2 + 5 - playerVF, playerVF, playerVF);
            self.playShowV.frame = CGRectMake(0, SCREEN_HEIGHT / 2 + 5,  SCREEN_WIDTH, SCREEN_HEIGHT / 2 + 50);
        } completion:nil];
        [UIView animateWithDuration:0.05 delay:0.65 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseOut animations:^{
            self.playerV.frame = CGRectMake(playerVF * 2, SCREEN_HEIGHT / 2 - playerVF, playerVF, playerVF);
            self.playShowV.frame = CGRectMake(0, SCREEN_HEIGHT / 2, SCREEN_WIDTH, SCREEN_HEIGHT / 2 + 50);
        } completion:nil];
        
        _isShowPlayerShowV = YES;
    } else {
        
        self.playerV.layer.shadowColor = [[UIColor clearColor] CGColor];
        
        self.playShowV.layer.shadowColor = [[UIColor clearColor] CGColor];

        
        [self.backV removeFromSuperview];
        
        [UIView animateWithDuration:0.4 animations:^{
            self.playerV.playerBtn.layer.transform = CATransform3DMakeRotation(0, 0, 0, 1);
        }];
        
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseOut animations:^{
            
            self.playerV.frame = CGRectMake(playerVF * 2, SCREEN_HEIGHT + 30 - playerVF, playerVF, playerVF);
            self.playShowV.frame = CGRectMake(0, SCREEN_HEIGHT + 30, SCREEN_WIDTH, SCREEN_HEIGHT / 2 + 40);
        } completion:nil];
        [UIView animateWithDuration:0.2 delay:0.2 options:UIViewAnimationOptionBeginFromCurrentState |  UIViewAnimationOptionCurveEaseOut   animations:^{
            self.playerV.frame = CGRectMake(playerVF * 2, SCREEN_HEIGHT - 10 - playerVF, playerVF, playerVF);
            self.playShowV.frame = CGRectMake(0, SCREEN_HEIGHT - 10,  SCREEN_WIDTH, SCREEN_HEIGHT / 2 + 40);
        } completion:nil];
        [UIView animateWithDuration:0.1 delay:0.4 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseOut animations:^{
            self.playerV.frame = CGRectMake(playerVF * 2, SCREEN_HEIGHT - playerVF, playerVF, playerVF);
            self.playShowV.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT / 2 + 100);
        } completion:nil];
        
        _isShowPlayerShowV = NO;
    }

}

//CATransform3D CATransform3DMakePerspective(CGPoint center, float disZ)
//{
//    CATransform3D transToCenter = CATransform3DMakeTranslation(-center.x, -center.y, 0);
//    CATransform3D transBack = CATransform3DMakeTranslation(center.x, center.y, 0);
//    CATransform3D scale = CATransform3DIdentity;
//    scale.m34 = -1.0f/disZ;
//    return CATransform3DConcat(CATransform3DConcat(transToCenter, scale), transBack);
//}
//
//CATransform3D CATransform3DPerspect(CATransform3D t, CGPoint center, float disZ)
//{
//    return CATransform3DConcat(t, CATransform3DMakePerspective(center, disZ));
//}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    tabBarController.navigationItem.title = viewController.tabBarItem.title;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
